<template>
	<view>
		<!-- #ifdef MP -->
		<mp-search-box></mp-search-box>
		<!-- #endif -->
		<notify></notify>
		<group-list :groupData="newestData"></group-list>
	</view>
</template>

<script>
	// #ifdef MP
	import mpSearchBox from '@/components/mp-search-box.vue'
	// #endif
	import notify from '@/components/notify.vue'
	import groupList from '@/components/group-list.vue'
	import api from '@/api/index.js'
	export default {
		data() {
			return {
				h: 24,
				page: 1,
				newestData: {
					type_id: null,
					type_name: '',
					list: []
				}
			}
		},
		created() {
			this.load(this.page, this.h)
		},
		components: {
			// #ifdef MP
			mpSearchBox,
			// #endif
			notify,
			groupList
		},
		methods: {
			async load(pg, h) {
				let {
					type_name,
					type_id,
					list,
					pagecount
				} = await api.newestData(pg, h)
				this.newestData.type_id = type_id
				this.newestData.type_name = type_name
				if (pg > pagecount) {
					uni.showToast({
						title: '已是最后一页',
						duration: 2000
					});
					this.page = pagecount
					return
				}
				this.newestData.list = this.newestData.list.concat(list)
			},
			async reload(pg, h) {
				let {
					type_name,
					type_id,
					list
				} = await api.newestData(pg, h)
				this.newestData.type_id = type_id
				this.newestData.type_name = type_name
				this.newestData.list = list
			}
		},
		// #ifndef MP
		onNavigationBarSearchInputClicked(e) {
			uni.navigateTo({
				url:'../search/search'
			})
		},
		onNavigationBarButtonTap(e) {
		   let index = e.index
			if(index==1){
				uni.navigateTo({
					url:'../history/history'
				})
			}
		},
		// #endif
		onPullDownRefresh() {
			this.page = 1
			this.reload(this.page, this.h)
			uni.stopPullDownRefresh();
		},
		onReachBottom() {
			this.page = this.page + 1
			this.load(this.page, this.h)
		}
	}
</script>

<style>

</style>
